package com.sduept.nwld.dataserver.controller.equipment.assessment;

import com.sduept.bigdata.equipment.assessment.dto.InspectSelfTreeTableDTO;
import com.sduept.bigdata.equipment.assessment.exception.ParameterException;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionPlan;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionStation;
import com.sduept.bigdata.equipment.assessment.service.InspectionPlanService;
import com.sduept.bigdata.equipment.assessment.service.InspectionStationService;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
/**
 * 自查计划-查看弹窗控制器
 * @author pengz
 *
 */
@Named
@ViewScoped
public class LookupInspectSelfPlanController extends AbstractController{

	private static final long serialVersionUID = -7297909177521397996L;
	@Autowired
	private InspectionPlanService planService;
	@Autowired
	private InspectionStationService stationService;
	/**
	 * 传入的参数：计划id
	 */
	private String planId;
	/**
	 * 需要查看的计划
	 */
	private InspectionPlan currentPlan;
	/**
	 * 计划下的评价厂站信息
	 */
	private Set<InspectionStation> currentInspectionStations;
	/**
	 * 页面展示模型集合
	 */
	private List<InspectSelfTreeTableDTO> targetModels;
	{
		targetModels = new ArrayList<InspectSelfTreeTableDTO>();
	}
	@PostConstruct
	public void init() {
		initParamters();
		initData();
	}
	/**
	 * 初始化参数
	 */
	private void initParamters() {
		planId = getParameter("id");
		
	}
	/**
	 * 初始化数据
	 */
	private void initData() {
		if(StringUtils.isNotBlank(planId)) {
			currentPlan = planService.findById(planId);
			try {
				currentInspectionStations = stationService.findByInspectionPlan(planId);
				initTargetModels(currentInspectionStations);
				sortModelList(targetModels);
			} catch (ParameterException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 初始化targetModels
	 * @param inspectionStations
	 */
	private void initTargetModels(Set<InspectionStation> inspectionStations) {
		for(InspectionStation instation : inspectionStations) {
			InspectSelfTreeTableDTO model = new InspectSelfTreeTableDTO();
			model.setAcceptScope(instation.getAcceptScope());
			model.setAcceptUnit(instation.getAcceptUnit());
			model.setStationName(instation.getStationName());
			model.setInspectExpert(instation.getInspectionExpert());
			model.setInspectExpertPhone(instation.getInspectionExpertPhone());
			model.setInspectionUnit(instation.getInspectionUnit());
			model.setActualStartTime(instation.getActualStartTime());
			model.setActualEndTime(instation.getActualEndTime());
			model.setStationPersonnel(instation.getStationPersonnel());
			model.setStationPersonnelPhone(instation.getStationPersonnelPhone());
			if(instation.getStationType()!=null) {
				model.setStationType(instation.getStationType().getLabel());
			}
			switch (instation.getStatus()) {
			case 0:
				model.setStatus("未开始");
				model.setStatusColor("blue");
				break;
			case 1:
				model.setStatus("进行中");
				model.setStatusColor("red");
				break;
			case 2:
				model.setStatus("已完成");
				model.setStatusColor("green");
				break;
			default:
				break;
			}
			targetModels.add(model);
		}
	}
	/**
	 * 对targetModels进行排序
	 * 排序规则：1、先按被检分子公司 2、在按被检单位
	 * @param selectLists
	 */
	private void sortModelList(List<InspectSelfTreeTableDTO> selectLists) {
		selectLists.sort(Comparator.comparing(InspectSelfTreeTableDTO::getAcceptScope)
				.thenComparing(InspectSelfTreeTableDTO::getAcceptUnit));
	}
	public List<InspectSelfTreeTableDTO> getTargetModels() {
		return targetModels;
	}
	public void setTargetModels(List<InspectSelfTreeTableDTO> targetModels) {
		this.targetModels = targetModels;
	}
	public InspectionPlan getCurrentPlan() {
		return currentPlan;
	}
	public void setCurrentPlan(InspectionPlan currentPlan) {
		this.currentPlan = currentPlan;
	}

}
